package com.xinnet.mms.web;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;

import com.xinnet.mms.entity.Notice;
import com.xinnet.mms.notice.service.NoticeService;
/**
 * 
 * 
 * @author 王湛智[wangzhanzhi@xinnet.com]
 *
 * @version 1.0
 *
 * @since 2014年2月19日
 *
 */
@Controller
@RequestMapping("/main")
public class MainController {
	@Autowired
	private NoticeService noticeService;
	
	/**
	 * 
	 * 功能描述：显示首页的政策公告
	 *
	 * @param request
	 * @param response
	 * @return
	 *
	 * @author 王湛智[wangzhanzhi@xinnet.com]
	 * @throws IOException 
	 * @throws SQLException 
	 *
	 * @since 2014年2月19日
	 *
	 * @update:[变更日期YYYY-MM-DD][更改人姓名][变更描述]
	 */
	@Transactional(propagation=Propagation.REQUIRED,readOnly=true)
	@RequestMapping("/mainPolicy")
	public String showPolicyInMain(HttpServletRequest request,HttpServletResponse response) throws IOException, SQLException {
		PrintWriter writer=response.getWriter();
		List<Notice> list=noticeService.getPolicyInMain();
		StringBuffer str=new StringBuffer("");
		for(Notice notice:list) {
			str.append(notice.getId()).append("$#");
			if(notice.getMsgTitle().length()>=6)
				str.append(notice.getMsgTitle().substring(0,6)+"...").append("$#");
			else
				str.append(notice.getMsgTitle()).append("$#");
			str.append(notice.getPubTime().substring(0, 10)).append("#$");;
		}
		writer.print(str.toString());
		return null;
	}
	/**
	 * 
	 * 功能描述：显示首页的动态消息
	 *
	 * @param request
	 * @param response
	 * @return
	 *
	 * @author 王湛智[wangzhanzhi@xinnet.com]
	 * @throws IOException 
	 * @throws SQLException 
	 *
	 * @since 2014年2月19日
	 *
	 * @update:[变更日期YYYY-MM-DD][更改人姓名][变更描述]
	 */
	@Transactional(propagation=Propagation.REQUIRED,readOnly=true)
	@RequestMapping("/mainMsg")
	public String showMsgInMain(HttpServletRequest request,HttpServletResponse response) throws IOException, SQLException {
		PrintWriter writer=response.getWriter();
		List<Notice> list=noticeService.getMsgInMain();
		StringBuffer str=new StringBuffer("");
		for(Notice notice:list) {
			str.append(notice.getId()).append("$#");
			if(notice.getMsgTitle().length()>=6)
				str.append(notice.getMsgTitle().substring(0,6)+"...").append("$#");
			else
				str.append(notice.getMsgTitle()).append("$#");
			str.append(notice.getPubTime().substring(0, 10)).append("#$");;
		}
		writer.print(str.toString());
		return null;
	}
}
